package com.shenyue.nbtownlet.activity;

import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;

import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityPdfBinding;
import com.shenyue.nbtownlet.model.DefaultModel;
import com.shenyue.nbtownlet.util.ActivityUtil;
import com.shenyue.nbtownlet.util.MD5Util;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;

import okhttp3.Call;

public class PDFActivity extends BaseActivity implements OnPageChangeListener, OnLoadCompleteListener {
    private ActivityPdfBinding binding;
    private String fileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_pdf);
        setActionBar(true, "PDF预览");

        Bundle bundle = getIntent().getExtras();
        final String url = bundle.getString(DefaultModel.URL);

        binding.progressLayout.showProgress();

        String mDDirPath = ActivityUtil.getMSDCardPath();
        mDDirPath = mDDirPath + "/" + DefaultModel.DOWNLOAD_DIR_NAME;
        File file = new File(mDDirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        fileName = MD5Util.md5(url) + ".pdf";
        File pdf = new File(mDDirPath + "/" + fileName);
        if (pdf.exists()) {
            openFile(pdf);
            return;
        }
        RequestCall call = OkHttpUtils.get().url(url).build();
        call.execute(new FileCallBack(mDDirPath, fileName) {
            @Override
            public void inProgress(float progress, long total, int id) {
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                openFile(url);
                binding.progressLayout.showContent();
            }

            @Override
            public void onResponse(File file, int id) {
                openFile(file);
            }
        });
    }

    private void openFile(File file) {
        binding.pdfView.fromFile(file)
                .defaultPage(0)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }

    private void openFile(String url) {
        binding.pdfView.fromUri(Uri.parse(url))
                .defaultPage(0)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }

    @Override
    public void onPageChanged(int i, int i1) {

    }

    @Override
    public void loadComplete(int i) {
        binding.progressLayout.showContent();
    }
}
